package com.kreig133.graphic;

import java.awt.Color;
import java.util.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;

/**
 * A simple demonstration application showing how to create a line chart using data from an
 * {@link XYDataset}.
 *
 */
public class GraphicFrame1 extends ChartPanel {

    String title;

    /**
     * Creates a new demo.
     *
     * @param title  the frame title.
     */
    public GraphicFrame1(final String title, Map<Double, Double> data) {

        super( createChart(title, createDataset(title, data)));
        
        this.setPreferredSize(new java.awt.Dimension(800, 600));
    }

    /**
     * Creates a sample dataset.
     *
     * @return a sample dataset.
     */
    static private XYDataset createDataset(String title, final Map<Double, Double> data) {

        final XYSeries series = new XYSeries(title);
        List<Double> temp = new ArrayList<Double>(data.keySet());
        Collections.sort(temp);
        for (int j = 0; j < temp.size(); j++) {
            series.add( (double) temp.get(j),(double) data.get(temp.get(j)));
        }

        final XYSeriesCollection dataset = new XYSeriesCollection();

        dataset.addSeries(series);

        return dataset;
    }

    static private JFreeChart createChart(String title, final XYDataset dataset) {

        // create the chart...
        final JFreeChart chart = ChartFactory.createXYLineChart(
                title, // chart title
                "y", // x axis label
                "t", // y axis label
                dataset, // data
                PlotOrientation.VERTICAL,
                true, // include legend
                true, // tooltips
                false // urls
                );

        chart.setBackgroundPaint(Color.white);

        final XYPlot plot = chart.getXYPlot();
        plot.setBackgroundPaint(Color.lightGray);
        //    plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);

        final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
//        renderer.setSeriesLinesVisible(0, false);
        renderer.setSeriesShapesVisible(0, false);

        plot.setRenderer(renderer);

        // change the auto tick unit selection to integer units only...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        // OPTIONAL CUSTOMISATION COMPLETED.

        return chart;
    }
}
